package com.loong.musicdemo.ui.find.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Song;

import java.util.List;

/**
 * 歌单中的歌曲adapter类
 */
public class AlbumSongAdapter extends RecyclerView.Adapter<AlbumSongAdapter.ViewHolder> {
    private List<Song> songs;

    public AlbumSongAdapter(List<Song> songs) {
        this.songs = songs;
    }

    @NonNull
    @Override
    public AlbumSongAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.album_song_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull AlbumSongAdapter.ViewHolder holder, int position) {
        Song song = songs.get(position);
        holder.song_order.setText(position + 1 + "");
        holder.song_name.setText(song.getName());
        holder.song_info.setText(song.getSingerName() + " - " + song.getAlbumName());
        //立即播放
        holder.playNow.setOnClickListener(v -> {
            if (onClickPlayButton != null) {
                onClickPlayButton.onPlayNowClick(position);
            }
        });
        //添加到播放列表
        holder.addToList.setOnClickListener(v -> {
            if (onClickPlayButton != null) {
                onClickPlayButton.onAddToList(position);
            }
        });
    }

    public interface OnClickPlayButton {
        void onPlayNowClick(int position);//立即播放

        void onAddToList(int position);//添加到播放列表
    }

    private OnClickPlayButton onClickPlayButton;

    public void setClickListener(OnClickPlayButton onClickPlayButton) {
        this.onClickPlayButton = onClickPlayButton;
    }

    @Override
    public int getItemCount() {
        return songs.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView song_order, song_name, song_info;
        ImageView playNow, addToList;//立刻播放 添加到播放列表

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            song_info = itemView.findViewById(R.id.song_info);
            song_order = itemView.findViewById(R.id.song_order);
            song_name = itemView.findViewById(R.id.song_name);
            playNow = itemView.findViewById(R.id.recommend_song_play);
            addToList = itemView.findViewById(R.id.song_setting);
        }
    }
}
